//
//  LoginViewController.m
//  Drivinglicense
//
//  Created by HappyMilk 02 on 2017/6/30.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginHeader.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *btn_type_log;
@property (weak, nonatomic) IBOutlet UIButton *btn_type_register;
@property (weak, nonatomic) IBOutlet UITextField *tf_username;
@property (weak, nonatomic) IBOutlet UITextField *tf_psd;
@property (weak, nonatomic) IBOutlet UIButton *btn_log_regis;

@property (nonatomic ) NSInteger type;

@end

@implementation LoginViewController
- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//- (instancetype)initWithCoder:(NSCoder *)aDecoder{
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        ADD_OBSERVER(self, @selector(showWXLoginBtnIfNeed), NOTIFICATION_APP_INFO_LOADED, nil);
//    }
//    return self;
//}
- (void)viewDidLoad {
    [super viewDidLoad];self.type = 0;//0 login   1  register
    // Do any additional setup after loading the view.
    
    self.btn_type_log.layer.masksToBounds = YES;
    self.btn_type_log.layer.cornerRadius = 10;
    self.btn_type_register.layer.masksToBounds = YES;
    self.btn_type_register.layer.cornerRadius = 10;
    
    [self showWXLoginBtnIfNeed];
    
}
- (void)showWXLoginBtnIfNeed{
    if(IS_PASS && IS_CHINESE && IS_IPAD && [[MPWeChat stander] m_needLogin]){
        
        UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){__kwidth/2-40,self.btn_log_regis.frame.origin.y+80,80,80}];
        [btn setImage:[UIImage imageNamed:@"wx_logo.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(wxLoginAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}
- (void)wxLoginAction{
    QRCodeLoginViewController * qrCodeLoginViewController = [[QRCodeLoginViewController alloc]initWithNibName:@"QRCodeLoginViewController" bundle:nil];
    [self presentViewController:qrCodeLoginViewController animated:true completion:nil];
}

- (IBAction)loginOrRegisterType:(UIButton *)sender {
    if (sender.tag == 0) {
        _type = 0;
        [self.btn_type_log setBackgroundColor:[UIColor whiteColor]];
        [self.btn_type_log setTitleColor:HEXCOLOR(0x35B2BA) forState:UIControlStateNormal];
        [self.btn_type_register setBackgroundColor:[UIColor clearColor]];
        [self.btn_type_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.btn_log_regis setTitle:@"登陆" forState:UIControlStateNormal];
    }else{
        if (IS_PASS && IS_CHINESE && IS_IPAD) {
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器维护暂时停用注册功能！老用户可正常登录！新用户请使用第三方登录！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertview show];
            return;
        }
        _type = 1;
        [self.btn_type_log setBackgroundColor:[UIColor clearColor]];
        [self.btn_type_log setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.btn_type_register setBackgroundColor:[UIColor whiteColor]];
        [self.btn_type_register setTitleColor:HEXCOLOR(0x35B2BA) forState:UIControlStateNormal];
        [self.btn_log_regis setTitle:@"注册" forState:UIControlStateNormal];
    }
}

- (IBAction)loginAction:(UIButton *)sender {
    if (_type == 0) {
        [self login];
    }else{
        [self registerAction];
    }
}

//登陆
- (void)login{

    NSString *username = _tf_username.text;
    NSString *password = _tf_psd.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = @"请输入正确的账号和密码，账号和密码不能低于六位";
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    
    SHOW_HUD(@"正在登陆...");
    [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
        HIDE_HUD
        if (user != nil) {
            [MPWeChat stander].m_isLogin = true;
            [self dismissViewControllerAnimated:true completion:nil];
            
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertview show];
            
        } else {
            SHOW_HUD_MESSAGE(@"账号或密码错误");
        }
        
    }];
}

//注册
- (void)registerAction{
    
    
    NSString *username = _tf_username.text;
    NSString *password = _tf_psd.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = @"请输入正确的账号和密码，账号和密码不能低于六位";
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    SHOW_HUD(@"正在注册...");
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = username;// 设置用户名
    user.password =  password;// 设置密码
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        HIDE_HUD
        if (succeeded) {
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功，请登陆" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertview show];
            
            
            //[self.navigationController popViewControllerAnimated:true];
        } else {
            SHOW_HUD_MESSAGE(@"注册失败");
        }
        
        NSLog(@"%@",error);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.tf_username resignFirstResponder];
    [self.tf_psd resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
